package org.jeecg.modules.pos.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.pos.PosBookingStateEnum;
import org.jeecg.modules.pos.entity.PosBookingOrder;
import org.jeecg.modules.pos.entity.PosOrderGoods;
import org.jeecg.modules.pos.mapper.PosBookingOrderMapper;
import org.jeecg.modules.pos.service.IPosBookingService;
import org.jeecg.modules.pos.service.IPosOrderGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PosBookingOrderImpl  extends ServiceImpl<PosBookingOrderMapper, PosBookingOrder> implements IPosBookingService {


    @Override
    public Page<PosBookingOrder> getBookingPage(PosBookingOrder bookingOrder, Page<PosBookingOrder> page, Date startTime, Date endTime) {
        LambdaQueryWrapper<PosBookingOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(bookingOrder.getHotelId()),PosBookingOrder::getHotelId, bookingOrder.getHotelId());
        wrapper.eq(ObjectUtil.isNotEmpty(bookingOrder.getState()),PosBookingOrder::getState, bookingOrder.getState());
        wrapper.eq(ObjectUtil.isNotEmpty(bookingOrder.getCode()),PosBookingOrder::getCode, bookingOrder.getCode());
        wrapper.eq(ObjectUtil.isNotEmpty(bookingOrder.getCustomerName()),PosBookingOrder::getCustomerName, bookingOrder.getCustomerName());
        wrapper.eq(ObjectUtil.isNotEmpty(bookingOrder.getPhone()),PosBookingOrder::getPhone, bookingOrder.getPhone());
        wrapper.ge(ObjectUtil.isNotEmpty(startTime),PosBookingOrder::getArriveTime, startTime);
        wrapper.le(ObjectUtil.isNotEmpty(endTime),PosBookingOrder::getArriveTime, endTime);
        return page(page, wrapper);
    }

    @Override
    public List<PosBookingOrder> getTodayBooking() {
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.set(Calendar.HOUR,0);
        nowCalendar.set(Calendar.MINUTE,0);
        nowCalendar.set(Calendar.SECOND,0);
        nowCalendar.set(Calendar.MILLISECOND,0);
        Calendar tomorrowCalendar = Calendar.getInstance();
        tomorrowCalendar.setTime(nowCalendar.getTime());
        tomorrowCalendar.add(Calendar.DAY_OF_YEAR,1);
        List<PosBookingOrder> bookingOrders = list(Wrappers.<PosBookingOrder>lambdaQuery()
                .eq(PosBookingOrder::getState, PosBookingStateEnum.NO_ARRIVE.getKey())
                .ge(PosBookingOrder::getArriveTime, nowCalendar.getTime()).le(PosBookingOrder::getArriveTime,tomorrowCalendar.getTime()));
        return bookingOrders;
    }
}
